package app.pumpkin.com.myandroiddemo.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import app.pumpkin.com.myandroiddemo.R;

/**
 * @author wangshy
 */
public class BroadcastActivity extends AppCompatActivity {
    BroadcastActivityReceiver broadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast);
    }

    @Override
    protected void onResume() {
        super.onResume();
        broadcastReceiver = new BroadcastActivityReceiver();
        IntentFilter filter = new IntentFilter();
        //设置优先级
        filter.setPriority(20);
        filter.addAction("app.pumpkin.com.myandroiddemo.receiver.BroadcastActivity");
        //注册广播
        registerReceiver(broadcastReceiver, filter);
    }

    public void check(View view) {
        switch (view.getId()) {
            case R.id.button:
                Intent intent = new Intent();
                intent.setAction("app.pumpkin.com.myandroiddemo.receiver.MyBroadcastReceiver");
                intent.putExtra("key", "别让后面那货知道");
                sendOrderedBroadcast(intent, null);
                break;
            case R.id.button4:
                sendBroadcast(new Intent().setAction("app.pumpkin.com.myandroiddemo.receiver.MyBroadcastReceiver"));
                break;
            case R.id.button3:
                sendBroadcast(new Intent().setAction("app.pumpkin.com.myandroiddemo.receiver.BroadcastActivity"));
                break;
            default:

                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(broadcastReceiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private class BroadcastActivityReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("wangshy", "---->>>> (BroadcastActivityReceiver.java:68)" + "动态注册广播已接收");
        }
    }
}
